package com.rabbiter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.common.QueryPageParam;
import com.rabbiter.common.Result;
import com.rabbiter.entity.Carrier;
import com.rabbiter.entity.Storage;
import com.rabbiter.service.CarrierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/carrier")
@Slf4j
public class CarrierController {

    @Resource
    private CarrierService carrierService;

    /**
     * 新增
     * @param carrier
     */
    @RequestMapping("/save")
    public Result save(@RequestBody Carrier carrier) {
        try {
            carrierService.save(carrier);
            return Result.success("新增成功");
        } catch (Exception e) {
            log.info("出错了:{}", e.getMessage());
            return Result.fail();
        }
    }

    /**
     * 更新
     * @param carrier
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Carrier carrier) {
        try {
            carrierService.updateById(carrier);
            return Result.success();
        } catch (Exception e) {
            log.info("出错了:{}", e.getMessage());
            return Result.fail();
        }
    }

    /**
     * 删除
     * @param carrier
     */
    @RequestMapping("/del")
    public Result delete(@RequestBody Carrier carrier) {
        try {
            carrierService.removeById(carrier);
            return Result.success();
        } catch (Exception e) {
            log.info("出错了:{}", e.getMessage());
            return Result.fail();
        }
    }


    /*
     * 模糊查询：根据输入查询并以分页的形式展示
     */
//    @PostMapping("/listPage")
//    public Result listPage(@RequestBody QueryPageParam query){
//        HashMap param = query.getParam();
//        String name = (String)param.get("name");
//
//        Page<Carrier> page = new Page();
//        page.setCurrent(query.getPageNum());
//        page.setSize(query.getPageSize());
//
//        LambdaQueryWrapper<Carrier> queryWrapper = new LambdaQueryWrapper<>();
//        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
//            queryWrapper.like(Carrier::getName,name);
//        }
//
//        IPage result = carrierService.pageCC(page,queryWrapper);
//        return Result.success(result.getRecords(),result.getTotal());
//    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        log.info("接收到的查询参数: {}", query);
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<Carrier> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Carrier> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            queryWrapper.like(Carrier::getName, name);
        }

        try {
            IPage<Carrier> result = carrierService.page(page, queryWrapper);
            log.info("查询结果: {}", result.getRecords());
            ArrayList<Object> list = new ArrayList<>();
            list.add(result.getRecords());
            list.add(result.getTotal());
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询过程出错", e);
            return Result.fail("查询过程出错: " + e.getMessage());
        }
    }



}
